$.fn.serializeObject = function()
{
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name] !== undefined) {
			if (!o[this.name].push) {
				o[this.name] = [o[this.name]];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};

$.extend({
	'crudgrid':{
		'getGrid' :function(gridBtn){
			return $(gridBtn).parent().closest('div.datagrid-wrap').children('div.datagrid-view').children('table.datagrid-f');
		},
		'buttons': {
			'create':{
				text: '新增' ,
				iconCls : 'icon-add',
				handler : function(){
					var $grid=$.crudgrid.getGrid(this);
					var $dialog=$('<div id=dialog />');
					$dialog.appendTo('body');
					var $options=$grid.datagrid('options');
					var $param={
							title : '新增'  ,
							width : 260 ,
							href : 'edit',
							modal: true,
							grid : $grid,
							onClose : function(){
								$dialog.dialog('destroy');
							}, 
							buttons:[$.dialog.buttons.save,$.dialog.buttons.reset,$.dialog.buttons.close]
						};
					if($options.dialog){
						$param=$.extend($param,$options.dialog.create);
					}
					$dialog.dialog($param);
				}
			},
			'update':{
				text:'编辑',
				iconCls:'icon-edit',
				handler:function(){
					var $grid=$.crudgrid.getGrid(this);
					var row=$grid.datagrid('getSelected');
					if(row==null){
						$.messager.alert({
							title: '提示' ,
							msg: '请先选择一行，再进行编辑操作' ,
							icon: 'info'
						});
						return;
					}
					var $dialog=$('<div id="dialog"/>');
					$dialog.appendTo($grid.parent());
					// 用前台的dialog.update去覆盖默认对话框属性
					var $param=$.extend({
						title:'编辑',
						width:260,
						grid : $grid,
						modal:true,
						href:'edit',
						queryParams: {id:row.id},
						onClose: function(){
							$dialog.dialog('destroy');
						},
						buttons:[$.dialog.buttons.save,$.dialog.buttons.reset,$.dialog.buttons.close]
					},$grid.datagrid('options').dialog?$grid.datagrid('options').dialog.update:{});
					//对话框初始化
					$dialog.dialog($param);
				}
			},
			'delete':{
				text: '删除' ,
				iconCls: 'icon-remove',
				handler: function(){
					var $table=$(this).parent().closest('.panel').find('.datagrid-f');
					var row=$table.datagrid('getSelected');
					if(row==null){
						$.messager.alert({
							title: '提示' ,
							msg: '请先选中一行，再进行删除操作操作！' ,
							icon : 'info'
						});
						return ;
					}
					$.messager.confirm('确认对话框', '您想要删除选中的数据吗？', function(r){
						if(r){
							$.post('delete',{id:row.id},function(result){
								if(result.success){
									/*$.messager.show({
										title: '提示',
										msg: result.msg,
										icon: 'info' 
									});*/
									toastr.warning(result.msg);
									/*toastr.success(result.msg);*/
									if($table.data('treegrid')){
										$table.treegrid('reload');
										$table.treegrid('uncheckAll');
									}else{
										$table.datagrid('reload');
										$table.datagrid('uncheckAll');
									}
								}else{
									/*$.messager.alert({
										title:'提示',
										msg: result.msg,
										icon: 'error'
									});*/
									toastr.error(result.msg);
								}
							});
						}
					}); 
				}
			},
			'search':{
				text:'查询' ,
				iconCls: 'icon-search' ,
				handler:function(){
					var $grid=$.crudgrid.getGrid(this);
					var $param=$grid.datagrid('options').queryParams;
					var $dialog=$("<div/>");
					$dialog.appendTo($grid.parent());
					$options=$grid.datagrid('options');
					$param=$.extend({
						title : '查询' ,
						width : 260 ,
						grid : $grid,
						href : 'search',
						queryParams:$param,
						onClose : function(){
							$dialog.dialog('destroy');
						},
						buttons: [$.dialog.buttons.search,$.dialog.buttons.clear,$.dialog.buttons.close]
					},$options.dialog?$options.dialog.search:{});
					$dialog.dialog($param);
				}
			},
			'reload':{
				text:'刷新' ,
				iconCls: 'icon-reload',
				handler: function(){
					var $grid=$.crudgrid.getGrid(this);
					if($grid.data('treegrid')){
						$grid.treegrid('unselectAll');
						$grid.treegrid('reload');
					}else{
						$grid.datagrid('unselectAll');
						$grid.datagrid('reload');
					}
				}
			}
		}
	}
	
});

$.extend({
	'dialog':{
		'me':function(dialogBtn){
			return $(dialogBtn).closest('.window').find('div.window-body');
		},
		'getForm':function(dialogBtn){
			return $(dialogBtn==undefined ? this :dialogBtn).parent().closest('.window').find('form');
		},
		'buttons':{
			'reset':{
				text:'重置',
				iconCls:'icon-reload',
				handler: function(){
					var $form=$.dialog.getForm(this);
					$form.form('reset');
				}
			},
			'save':{
				text:'保存',
				iconCls:'icon-ok',
				handler: function(){
					$this=$(this);
					$this.linkbutton('disable');
					var $dialog=$.dialog.me(this);
					var $form=$.dialog.getForm(this);
					var $table=$.dialog.me(this).dialog('options').grid;
					var $action=$dialog.dialog('options').action;
					var fd= new FormData($form[0]);
					if($form.form('validate')){
						$.ajax({
							url: $action?$action:'save',
							cache: false ,
							type : 'post',
							data : fd ,
							dataType : 'json' ,
							processData: false,
							contentType: false,
							success : function(result){
								if(result.success){
									/*$.messager.show({
										title: '提示'  ,
										msg : result.msg ,
										icon : 'info' 
									});*/
									toastr.success(result.msg);
									$dialog.dialog('close');
									if($table.data('treegrid')){
										$table.treegrid('reload');
									}else{
										$table.datagrid('reload');
									}
								}else{
									$.messager.alert({
										title: '提示'  ,
										msg : result.msg ,
										icon : 'error'
									});
									$this.linkbutton('enable');
								}
							}
						});
					}else{
						$this.linkbutton('enable');
					}
				}
			},
			'close':{
				text:'关闭',
				iconCls:'icon-cancel',
				handler: function(){
					$.dialog.me(this).dialog('close');
				}
			},
			'search':{
				text:'查询' ,
				iconCls : 'icon-search',
				handler : function(){
					var $dialog=$.dialog.me(this);
					var $form=$.dialog.getForm(this);
					var $table=$.dialog.me(this).dialog('options').grid;
					$table.datagrid('load',$form.serializeObject());
					$dialog.dialog('close');
					
				}
			},
			'clear': {
				text : '清除条件' ,
				iconCls : 'icon-clear' ,
				handler : function(){
					var $form=$.dialog.getForm(this);
					var $table=$.dialog.me(this).dialog('options').grid;
					$form.form('clear');
					$table.datagrid('load',{});
				}
			}
		}
	}
});